﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Text;
using System.Web.UI.HtmlControls;

namespace CoolERP_Web.User
{
    public partial class CS_CPX_Query : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        #region=======GridView处理=======
        private void BindData()
        {
            using (CS_CPXBLL cBLL = new CS_CPXBLL())
            {
                GridView1.DataSource = cBLL.GetAll();
                GridView1.DataBind();
            }
            if (Request.QueryString["vpath"] != null)
            {
                this.btnAdd.Visible = false;
                this.btnDel.Visible = false;
                this.GridView1.Columns[0].Visible = false;
                this.GridView1.Columns[3].Visible = false;
            }
        }

        protected void GridView1_PreRender(object sender, EventArgs e)
        {
            if (GridView1.Rows.Count == 0)
                Utility.RenderEmptyGridView(GridView1, "BH,MC");
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindData();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtBH = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtBH"));
            TextBox txtMC = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtMC"));
            using (CS_CPXBLL cBLL = new CS_CPXBLL())
            {
                CS_CPX item = cBLL.GetEntity(t => t.BH == txtBH.Text.Trim());
                item.BH = txtBH.Text.Trim();
                item.MC = txtMC.Text.Trim();
                cBLL.Update(item);
            }
            GridView1.EditIndex = -1;
            BindData();
        }

        protected void btnDel_Click(object sender, ImageClickEventArgs e)
        {
            StringBuilder sbIds = new StringBuilder();
            foreach (GridViewRow container in GridView1.Rows)
            {
                HtmlInputCheckBox cBox = (HtmlInputCheckBox)container.FindControl("chkSel");
                if (cBox != null && cBox.Checked)
                    sbIds.Append(string.Format(",{0}", cBox.Value));
            }
            if (sbIds.Length > 0)
            {
                sbIds = sbIds.Remove(0, 1);
                using (CS_CPXBLL cBLL = new CS_CPXBLL())
                {
                    cBLL.Deletes("BH", sbIds.ToString()) ;
                }
                BindData();
            }
        }
        #endregion

        #region=======添加产品线=======
        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.ShowFooter = true;
            BindData();
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            TextBox txtBH = GridView1.FooterRow.FindControl("txtBH") as TextBox;
            TextBox txtMC = GridView1.FooterRow.FindControl("txtMC") as TextBox;
            using (CS_CPXBLL cBLL = new CS_CPXBLL())
            {
                CS_CPX item = new CS_CPX();
                item.BH = txtBH.Text.Trim();
                item.MC = txtMC.Text.Trim();
                cBLL.Insert(item);
            }
            GridView1.ShowFooter = false;
            BindData();
        }

        protected void btnCancel_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.ShowFooter = false;
            BindData();
        }
        #endregion
    }
}